package org.jetlinks.community.device.web.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import org.hswebframework.reactor.excel.CellDataType;
import org.hswebframework.reactor.excel.ExcelHeader;
import org.jetlinks.community.device.entity.DemoEntity;
import org.jetlinks.core.metadata.ConfigPropertyMetadata;
import org.jetlinks.core.metadata.PropertyMetadata;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Data
public class DemoExportInfo {

    @ExcelProperty("标题")
    private String title;

    @ExcelProperty("内容")
    private String content;

    @ExcelProperty("时间")
    private Date date;

    private List<DemoEntity> tags = new ArrayList<>();
    public static List<ExcelHeader> getExportHeaderMapping(List<PropertyMetadata> tags,
                                                           List<ConfigPropertyMetadata> configs) {
        List<ExcelHeader> arr = new ArrayList<>(Arrays.asList(
            new ExcelHeader("title", "标题", CellDataType.STRING),
            new ExcelHeader("content", "内容", CellDataType.STRING),
            new ExcelHeader("date", "时间", CellDataType.STRING)
        ));
        for (PropertyMetadata tag : tags) {
            arr.add(new ExcelHeader(tag.getId(), StringUtils.isEmpty(tag.getName()) ? tag.getId() : tag.getName(), CellDataType.STRING));
        }
        for (ConfigPropertyMetadata config : configs) {
            arr.add(new ExcelHeader("configuration." + config.getProperty(),
                StringUtils.isEmpty(config.getName()) ? config.getProperty() : config.getName(),
                CellDataType.STRING));
        }
        return arr;
    }

    public DemoEntity toDemoInfo() {
        DemoEntity demoInfo = new DemoEntity();
        demoInfo.setTitle(title);
        demoInfo.setContent(content);
        demoInfo.setDate(date);
        return demoInfo;
    }

}
